//
//  UIButton+Extexsion.swift
//  新浪微博
//
//  Created by 焦林生 on 15/11/4.
//  Copyright © 2015年 焦林生. All rights reserved.
//

import UIKit

extension UIButton {
    /**
    便利构造函数
    
    - parameter title:     title
    - parameter fontSize:  size
    - parameter color:     字体颜色
    - parameter imageName: 图片名称
    - parameter backColor: 背景颜色
    
    - returns: UIButton
    */
    
    convenience init(title: String, fontSize: CGFloat, color: UIColor, imageName: String?, backColor: UIColor? = nil) {
    
        self.init()
        
        setTitle(title, forState: .Normal)
        setTitleColor(color, forState: .Normal)
        
        if let imageName = imageName {
        
            setImage(UIImage(named: imageName), forState: .Normal)
            
        }
        
        titleLabel?.font = UIFont.systemFontOfSize(fontSize)
        backgroundColor = backColor
        sizeToFit()
    }

    //便利构造函数
    convenience init(imageName: String, backImageName: String?) {
        
        self.init()
        
        setImage(UIImage(named: imageName), forState: UIControlState.Normal)
        setImage(UIImage(named: imageName + "_highlighted"), forState: UIControlState.Highlighted)
        if let backImageName = backImageName {
        
            setBackgroundImage(UIImage(named: backImageName), forState: UIControlState.Normal)
            setBackgroundImage(UIImage(named: backImageName + "_highlighted"), forState: UIControlState.Highlighted)
        }
      
        
        // 会根据背景图片的大小调整尺寸
        sizeToFit()
    }
    //注册 登陆按钮的代码重构
    convenience init(title: String, color: UIColor, imageName: String) {
    
        self.init()
        setTitle(title, forState: .Normal)
        setTitleColor(color, forState: .Normal)
        
        setBackgroundImage(UIImage(named: imageName), forState: .Normal)
        sizeToFit()
    }
    
    
    //便利构造函数
    convenience init(title: String, fontSize: CGFloat, color: UIColor, imageName: String) {
        self.init()
    
        setTitle(title, forState:UIControlState.Normal)
        setTitleColor(color, forState: UIControlState.Normal)
       setImage(UIImage(named: imageName), forState: .Normal)
        
        titleLabel?.font = UIFont.systemFontOfSize(fontSize)
        
        sizeToFit()
    }
}
